*     *****************************************************************  * 
*     *****************************************************************  * 
*       File-Name:      panel.do                                         *  
*       Date:           12 August 2016                                   * 
*       Author:         Gschwend                                         * 
*       Purpose:      	Bavaria panel data to study strategic balancing  *
*                       Table 6.5 & Figure 6.4					         *
* 	    Input Files:    SURV_GER_Bavaria_5wave_cleaned_SurveyData.dta    *
*       Data Output:    panel.dta                                        *              
*     ****************************************************************   * 
*     ****************************************************************   * 

version 14.2
clear
capture log close
set more off




**********************************************
* Specify your own local path to the dropbox *
**********************************************


* Thomas G.'s local (office) path:
* local path /Users/gschwend/Dropbox/Andre/OUP multilevel electoral behavior book/chapter 6/replication/


* Thomas G.'s local (Air) path:
* local path /Users/thomasgschwend/Dropbox/Andre/OUP multilevel electoral behavior book/chapter 6/replication/
 
 
 


use "SURV_GER_Bavaria_5wave_cleaned_SurveyData", clear 




***********************
*** STATE ELECTION  ***
***********************

* vote choice state election

*Q6B_W1	83	Q6B.  Which party's list did you vote for? (already voted)
*Q7B_W1	90	Q7B.  For which of the following party lists will you vote on election day?

gen vote_by = Q6B_W1
replace vote_by = Q7B_W1 if vote_by==.
recode vote_by 88=9 98/99=.
label define vote 0 "CDU" 1 "CSU" 2 "SPD" 3 "B90" 4 "Freie Waehler" 5  "FDP" 6 "Linke" 7 "Piraten" 8 "AfD" 9 "Others"
label values vote_by vote


/* Party Identification & SPID
Q46_W1	225	Q46.  Do you usually think of yourself as close to any particular political party in Bavaria?
Q46A_W1	227	Q46A.  Which party do you feel closest to?
Q46B_W1	229	Q46B.  Do you feel very close to [political party in Bavaria], somewhat close, or not very close?
*/

gen pid_by = Q46A_W1
recode pid_by 88=9 98/99=.
*label define vote 0 "CDU" 1 "CSU" 2 "SPD" 3 "B90" 4 "Freie Waehler" 5  "FDP" 6 "Linke" 7 "Piraten" 8 "AfD" 9 "Others"
label values pid_by vote

gen spid_by = Q46B_W1
recode spid_by 9=0 .=0
label define spid  1 "Not very close" 2 "Somewhat close" 3 "Very close" 0 "No PID/DK" 
label values spid_by spid

*Q9A_W1	93	Q9A.  Which party did you vote for in the last Federal Election in September 2009?


*Q32B_W1	Q32B.  How much do you think the following care about what people like you think: The federal government?
*Q42B_W1	Q42B.  How much influence do the policies of the following governments have on the well being of you and your family: Federal government?




/* Feeling Thermometer (0-10)
Q17A_W1	109	Q17A.  Please rate each of the following political parties in Bavaria: CSU.
Q17B_W1	110	Q17B.  Please rate each of the following political parties in Bavaria: BayernSPD.
Q17C_W1	111	Q17C.  Please rate each of the following political parties in Bavaria: Greens.
Q17D_W1	112	Q17D.  Please rate each of the following political parties in Bavaria: Free Voters.
Q17E_W1	113	Q17E.  Please rate each of the following political parties in Bavaria: FDP.
Q17F_W1	114	Q17F.  Please rate each of the following political parties in Bavaria: Left.
Q17G_W1	115	Q17G.  Please rate each of the following political parties in Bavaria: Pirates.
*/

gen ftby_csu = Q17A_W1
gen ftby_spd = Q17B_W1
gen ftby_b90 = Q17C_W1
gen ftby_frw = Q17D_W1
gen ftby_fdp = Q17E_W1
gen ftby_lin = Q17F_W1
gen ftby_pir = Q17G_W1

mvdecode ftby_*, mv(99=.) /* get rid of missing values */


/* Left-Right
Q30AA.  Where would you place each of the following political parties in Bavaria on that scale: CSU?
Q30AB.  Where would you place each of the following political parties in Bavaria on that scale: SPD?
Q30AC.  Where would you place each of the following political parties in Bavaria on that scale: Greens?
Q30AD.  Where would you place each of the following political parties in Bavaria on that scale: Free Voters?
Q30AE.  Where would you place each of the following political parties in Bavaria on that scale: FDP?
Q30AF.  Where would you place each of the following political parties in Bavaria on that scale: Left?
Q30AG.  Where would you place each of the following political parties in Bavaria on that scale: Pirates?

Q30A.  Where would you place yourself on a 0 to 10 scale where 0 means far left and 10 means far right?
*/

gen lrby_csu = Q30AA_W1
gen lrby_spd = Q30AB_W1
gen lrby_b90 = Q30AC_W1
gen lrby_frw = Q30AD_W1
gen lrby_fdp = Q30AE_W1
gen lrby_lin = Q30AF_W1
gen lrby_pir = Q30AG_W1

gen lrby_sel = Q30A_W1
mvdecode lrby_*, mv(99=.) /* get rid of missing values */

* Distance: Party - Respondent (Bavaria)
gen distby_csu = lrby_csu  - lrby_sel
gen distby_spd = lrby_spd  - lrby_sel
gen distby_b90 = lrby_b90  - lrby_sel
gen distby_frw = lrby_frw  - lrby_sel
gen distby_fdp = lrby_fdp  - lrby_sel
gen distby_lin = lrby_lin  - lrby_sel
gen distby_pir = lrby_pir  - lrby_sel



/*
Q34A_W1	 Please rate the importance to you personally of these three elections: State elections.
Q34B_W1	 Please rate the importance to you personally of these three elections: Federal elections.
Q34C_W1	 Please rate the importance to you personally of these three elections: European elections.
*/

gen imp_sta1 = Q34A_W1
gen imp_fed1 = Q34B_W1
gen imp_eup1 = Q34C_W1

mvdecode imp_*, mv(99=.) /* get rid of missing values */








**************************
*** NATIONAL ELECTION  ***
**************************

* vote choice Bundestag election

*Q6B_W2	452	Q6B.  Which party's list did you vote for?
*Q7B_W2	457	Q7B.  For which of the following party lists will you vote on election day?

gen vote = Q6B_W2
replace vote = Q7B_W2 if vote==.
recode vote 88=9 98/99=.
*label define vote 1 "CSU" 2 "SPD" 3 "B90" 4 "Freie Waehler" 5  "FDP" 6 "Linke" 7 "Piraten" 8 "AfD" 9 "Others"
label values vote vote


/* Partisanship & SPID
Q47_W2	557	Q47.  Do you usually think of yourself as close to any particular federal political party?
Q47A_W2	559	Q47A.  Which party do you feel closest to?
Q47B_W2	561	Q47B.  Do you feel very close to [political party in Germany], somewhat close, or not very close?
*/



gen pid = Q47A_W2
recode pid 88=9 98/99=. 2=0 3=2 4=3 5=4 6=5 7=6 8=7 
*label define vote 0 "CDU" 1 "CSU" 2 "SPD" 3 "B90" 4 "Freie Waehler" 5  "FDP" 6 "Linke" 7 "Piraten" 8 "AfD" 9 "Others"
label values pid vote

gen spid = Q47B_W2
recode spid 9=0 .=0
*label define spid  1 "Not very close" 2 "Somewhat close" 3 "Very close" 0 "No PID/DK" 
label values spid spid

*Q14_W2		Q14.  How satisfied are you with the performance of the Federal government in Berlin?

ren   Q14_W2 govperform
recode govperform 9=.




/* Feeling Theormometer Bundestag election
Q17A_W2	472	Q17A.  Please rate each of the following federal political parties: CSU.
Q17B_W2	473	Q17B.  Please rate each of the following federal political parties: CDU.
Q17C_W2	474	Q17C.  Please rate each of the following federal political parties: SPD.
Q17D_W2	475	Q17D.  Please rate each of the following federal political parties: Greens.
Q17E_W2	476	Q17E.  Please rate each of the following federal political parties: Free Voters.
Q17F_W2	477	Q17F.  Please rate each of the following federal political parties: FDP.
Q17G_W2	478	Q17G.  Please rate each of the following federal political parties: Left.
Q17H_W2	479	Q17H.  Please rate each of the following federal political parties: Pirates.
Q17I_W2	480	Q17I.  Please rate each of the following federal political parties: AfD.
*/

gen ft_csu = Q17A_W2
gen ft_cdu = Q17B_W2
gen ft_spd = Q17C_W2
gen ft_b90 = Q17D_W2
gen ft_frw = Q17E_W2
gen ft_fdp = Q17F_W2
gen ft_lin = Q17G_W2
gen ft_pir = Q17H_W2
gen ft_afd = Q17I_W2

mvdecode ft_*, mv(99=.) /* get rid of missing values */


* Supporters of the national government (before 2013 election, i.e. CDU/FDP) that vote for an opposition party (i.e. not for CSU/FDP) in state election
* although they most prefer either CSU or FDP
* and vote for incumbent national government (CSU/FDP) 

egen maxft = rmax(ft_csu ft_cdu ft_spd ft_b90 ft_frw ft_fdp ft_lin ft_pir ft_afd)
gen govsupporter = 0
replace govsupporter = 1 if maxft==ft_csu | maxft==ft_cdu | maxft==ft_fdp 
replace govsupporter = . if maxft==.


label define govsupporter 0 "no" 1 "yes" 
label values govsupporter govsupporter
label variable govsupporter  "National Govt. supporter (most-prefers party of incumbent govt)"  




egen maxftby = rmax(ftby_csu ftby_spd ftby_b90 ftby_frw ftby_fdp ftby_lin ftby_pir)
gen govsupporterby = 0
replace govsupporterby = 1 if maxftby==ftby_csu | maxft== ftby_fdp
replace govsupporterby = . if maxftby==.

label define govsupporterby 0 "no" 1 "yes" 
label values govsupporterby govsupporterby
label variable govsupporterby  "State Govt. supporter (most-prefers party of incumbent state govt)"  



gen balance = govsupporter 
recode balance 1 = 0 if vote_by==1 | vote_by==5     // not voting for CSU/FDP
recode balance 1 = 0 if govsupporterby==0           // although most-prefering the former incumbent state government
recode balance 1 = 0 if inlist(vote, 2,3,4,6,7,8,9) // voting for incumbent national CSU/FDP
replace balance =. if  vote_by==. | vote==. | govsupporterby==. | govsupporter==. 

label define balance 0 "No" 1 "Yes" 
label values balance balance
label variable balance  "Balancing Voter"  

* Balancing voters are slightly and sig. more conservative (5.3 vs 6.0)

reg lrby_sel balance



* Idiological centrality of respondent (distance of self placement to party)
gen idioc1 = abs(lrby_sel - 5)

reg idioc1 balance 




gen balance1 = 0
replace balance1 = 1 if inlist(vote, 1,5) // voting for incumbent national CSU/FDP
recode balance1 1 = 0 if vote_by==1 | vote_by==5     // not voting for CSU/FDP in state electon
recode balance1 1 = 0 if govsupporterby==0           // although most-prefering the former incumbent state government
replace balance1 =. if  vote_by==. | vote==. | govsupporterby==.  

* Balancing voters are slightly and sig. more conservative (5.3 vs 6.0)
reg lrby_sel balance1
logit balance1 spid


* Supporters of the national opposition (before 2013 election, i.e. not CSU/FDP) that vote for parties of national govt (i.e.  for CSU/FDP) in state election
* although they most prefer neither CSU nor FDP
* and vote for  national opposition (i.e., not CSU/FDP) 

gen ugvoter = 0
replace ugvoter = 1 if inlist(vote, 2,3,4,6,7,8,9)        // voting for  national opposition (i.e., not for CSU/FDP)
recode ugvoter 1 = 0 if inlist(vote_by, 2,3,4,6,7,8,9)    //  voting for CSU/FDP in state electon
recode ugvoter 1 = 0 if govsupporterby==1                 // although most-prefering neither CSU nor FDP
recode ugvoter 1 = 0 if govsupporter==1                   // supporter of party in national opposition (i.e., not CSU/FDP)

replace ugvoter =. if  vote_by==. | vote==. | govsupporterby==. | govsupporter==. 




* 1/3 of all CSU voters prefer actually the CDU
gen csu = ft_csu - ft_cdu
 






/*
Q30AA_W2	Where would you place each of the following political parties in Germany on that scale: CSU?
Q30AB_W2	Where would you place each of the following political parties in Germany on that scale: CDU?
Q30AC_W2	Where would you place each of the following political parties in Germany on that scale: SPD?
Q30AD_W2	Where would you place each of the following political parties in Germany on that scale: Greens?
Q30AE_W2	Where would you place each of the following political parties in Germany on that scale: Free Voters?
Q30AF_W2	Where would you place each of the following political parties in Germany on that scale: FDP?
Q30AG_W2	Where would you place each of the following political parties in Germany on that scale: Left?
Q30AH_W2	Where would you place each of the following political parties in Germany on that scale: Pirates?
Q30AI_W2	Where would you place each of the following political parties in Germany on that scale: AfD?
*/

gen lr_csu = Q30AA_W2
gen lr_cdu = Q30AB_W2
gen lr_spd = Q30AC_W2
gen lr_b90 = Q30AD_W2
gen lr_frw = Q30AE_W2
gen lr_fdp = Q30AF_W2
gen lr_lin = Q30AG_W2
gen lr_pir = Q30AH_W2
gen lr_afd = Q30AI_W2
    
gen lr_sel = Q30A_W2
mvdecode lr_*, mv(99=.) /* get rid of missing values */

* Distance: Party - Respondent
gen dist_csu = abs(lr_csu - lr_sel)
gen dist_cdu = abs(lr_cdu - lr_sel)
gen dist_spd = abs(lr_spd - lr_sel)
gen dist_b90 = abs(lr_b90 - lr_sel)
gen dist_frw = abs(lr_frw - lr_sel)
gen dist_fdp = abs(lr_fdp - lr_sel)
gen dist_lin = abs(lr_lin - lr_sel)
gen dist_pir = abs(lr_pir - lr_sel)
gen dist_afd = abs(lr_afd - lr_sel)


* balancing voters should be less close to their preferred party idiologically than others
egen mindist = rmin(dist_csu dist_cdu dist_spd dist_b90 dist_frw dist_fdp dist_lin dist_pir dist_afd)



reg mindist balance
logit balance mindist


* Idiological centrality of respondent (distance of self placement to party)
gen idioc = abs(lr_sel - 5)

reg  lr_sel balance
reg  idioc balance


* balancers shout have idiol position between SPD and CSU

gen iposition = (lr_spd<=lr_csu & lr_spd<=lr_sel & lr_sel<=lr_csu)
replace iposition= . if (lr_spd==. | lr_csu==. | lr_sel==.)

gen iposition1 = (lr_spd<lr_csu & lr_spd<=lr_sel & lr_sel<=lr_csu)
replace iposition1= . if (lr_spd==. | lr_csu==. | lr_sel==.)

gen iposition2 = (lr_spd<lr_csu & lr_spd<lr_sel & lr_sel<lr_csu)
replace iposition2= . if (lr_spd==. | lr_csu==. | lr_sel==.)


* Difference between SPD and CSU

gen distlr = abs(lr_spd - lr_csu)
gen distft = abs(ft_spd - ft_csu)

reg distlr balance
reg distft balance


* aquiessence bias: respodents placing themselves and parties on the middle category


gen mid1 = (lr_csu == 5)
gen mid2 = (lr_cdu == 5)
gen mid3 = (lr_spd == 5)
gen mid4 = (lr_b90 == 5)
gen mid5 = (lr_frw == 5)
gen mid6 = (lr_fdp == 5)
gen mid7 = (lr_lin == 5)
gen mid8 = (lr_pir == 5)
gen mid9 = (lr_afd == 5) 
gen mid0 = (lr_sel == 5)


gen mid = mid0 + mid1 + mid2 + mid3 + mid4 + mid5 + mid6 + mid7 + mid8 + mid9






/*
Q34A_W2	551	Q34A.  Please rate the importance to you personally of these three elections: State elections.
Q34B_W2	552	Q34B.  Please rate the importance to you personally of these three elections: Federal elections.
Q34C_W2	553	Q34C.  Please rate the importance to you personally of these three elections: European elections.
*/

gen imp_sta2 = Q34A_W2
gen imp_fed2 = Q34B_W2
gen imp_eup2 = Q34C_W2

mvdecode imp_*, mv(99=.) /* get rid of missing values */


* difference importance between Federal and State elections less for balacing voters
gen deltaimp1 = imp_fed1 - imp_sta1 
gen deltaimp = imp_fed2 - imp_sta2 
gen deltaspid = deltaimp * spid
gen delta1spid = deltaimp1 * spid

reg deltaimp balance
reg deltaimp1 balance


gen deltafed = (imp_fed1 + imp_fed2)/2
gen deltasta = (imp_sta1 + imp_sta2)/2

gen dfedsta = abs(deltafed-deltasta)
reg dfedsta balance

* Relevant is abs. distance

gen delta = abs(deltaimp)
gen delta1 = abs(deltaimp1)



** Political Knowledge

* state level
gen polknow1 = (Q10A_W1==5)
gen polknow2 = (Q10B_W1==7)
gen polknow3 = (Q10C_W1==3)
gen polknow4 = (Q10D_W1==1)
*gen polknow5 = (Q10E_W1==1) /* does not scale */


* national-level
gen polknow6 = (Q10A_W2==5)
gen polknow7 = (Q10B_W2==7)
gen polknow8 = (Q10C_W2==9)
gen polknow9 = (Q10D_W2==1)
gen polknow0 = (Q10E_W2==3) 
alpha polknow1  polknow2 polknow3 polknow4  polknow6  polknow7 polknow8 polknow9 polknow0 if balance!=., detail item gen(polknow)


gen knowspid = polknow * spid

logit balance spid deltaimp idioc mindist polknow  mid
logit balance spid deltaimp1 idioc mindist polknow knowspid mid


* combine DG and UG voters
gen bal = balance + ugvoter

logit balance  distft 
logit balance  distft  govperform 
logit balance  distft  govperform spid polknow 



logit balance  distft 
estadd scalar LL = e(ll)
estat classification
estadd scalar PCP = r(P_corr)
eststo k1


logit balance  distft  govperform
estadd scalar LL = e(ll)
estat classification
estadd scalar PCP = r(P_corr)
eststo k2

logit balance  distft  govperform spid polknow 
estadd scalar LL = e(ll)
estat classification
estadd scalar PCP = r(P_corr)
eststo k3



fre balance if e(sample)





*****************
*** Table 6.5 ***
*****************


#delimit ;
esttab k2 k3  using balance, b(%9.2f) se(%9.2f) 
 varlabels(_cons "Constant" balance "Strategic Balancing" deltaimp1 "\u0916? Importance" govperform "Performance Nat. Govt." 
          spid "Strength of PID" polknow "Pol. Knowledge" mindist "Ideol. Proximity" distft "Party differential")  label noobs
		  title ("Table 6.5: Proclivity of Strategic Balancing, MEDW Panel")
 rtf replace nonotes addnote("N = `e(N)'; Standard errors in parentheses. * p < 0.05; ** p < 0.01")  star(* 0.05 ** 0.01) 
stats(LL PCP, fmt(%9.0f) ) 
modelwidth(10) 	
;
#delimit cr





logit balance  distft  govperform spid polknow 
gen esample = (e(sample)==1)
keep if esample==1
keep balance  distft  govperform spid polknow 
gen mergeid = _n	
compress								                 /* get id variable to merge in the simulated coefficients */
save balance.dta, replace



sum mergeid									       /* get N to draw as many simulations as you have observation in the data */

drawnorm b1-b5, seed(1234) n(`r(N)') means(e(b)) cov(e(V)) clear
gen mergeid = _n                                   /* get id variable to merge in the simulated coefficients */

merge 1:1 mergeid using balance.dta
tab _merge
drop _merge
compress
save graph_balance.dta, replace
rm balance.dta                              /* clean-up! Get rid of data we do not need anymore */



tempname sim           // temporary filename, ensures that the file will be erased 
 
#delimit ;
* We need to postfile some variables in order to make a nice graph.                   *;
* In particular we need expected value of both scenarios & their confidence intervall *;
* If we wanna calculate first-differences we need to postfile it, too                 *;			 
postfile `sim' ev0 plo0 pup0  using result, replace;

#delimit cr           
*     ******************************************************************************  *;
*       Start loop.  Let `a' be the values of the  key independent variable that is   *;
*  		represented  at the x-axis and let it run from 0,..., 10 in the desired      *;
* 		increments.                                                                   *;
*     ******************************************************************************  *;                                  


*** Step 2: Define scenario  (setx-step) *;

* For varying importance we have two different values for distft (0 & 10) *;
* and we set everything else to its mean and mode)                               *;

logit balance  distft  govperform spid polknow

#delimit ;
    gen x_0 =    b1*0
                   + b2*govperform
                   + b3*spid
	               + b4*polknow
                   + b5;

#delimit ;
    gen x_1 =    b1*10
                   + b2*govperform
                   + b3*spid
	               + b4*polknow
                   + b5;

#delimit cr 
  
*** Step 3: Simulate quantities of interest (simqi-step)  *;
  
                            
* Predicted probabilities given a logit model  *;
* P(turnout = 1) = exp(x_beta)/(1+exp(x_beta)) *;
* If you use a different model this will change *;





gen prob1=exp(x_1)/(1+exp(x_1))	/* Predicted Probs Scenario 1 */ 
gen prob0=exp(x_0)/(1+exp(x_0))	/* Predicted Probs Scenario 2 */ 

egen  p1 = mean(prob1)
egen  p0 = mean(prob0)


	* We are interested in a first-difference of those scenarios: *
    gen diff = prob0-prob1  /*  different values for the differences */
    egen  fd = mean(diff)

		
* lower & upper bound for first-difference	
	_pctile diff, p(2.5,97.5) 
    scalar lod = r(r1)
    scalar upd = r(r2) 


* lower & upper bound for expected value of scenario 0 (95% CI)*;
    _pctile prob0, p(2.5,97.5) 
    scalar lo0 = r(r1)
    scalar hi0 = r(r2)  
 
* lower & upper bound for expected value of scenario 1 (95% CI)*;
    _pctile prob1, p(2.5,97.5) 
    scalar lo1 = r(r1)
    scalar hi1 = r(r2)  

gen coef = .
replace coef = p0 in 3
replace coef = p1 in 2
replace coef = fd in 1
format coef %9.2g
 

gen up = .
replace up = hi0 in 3
replace up = hi1 in 2
replace up = upd in 1

gen lo = .
replace lo = lo0 in 3
replace lo = lo1 in 2
replace lo = lod in 1





******************
*** Figure 6.4 ***
******************


capture drop xaxis
gen xaxis = _n in 2/3
#delimit ;
twoway (scatter xaxis coef,  mcolor(black) mlabel(coef) mlabcolor(black) mlabposition(12) mlabgap(*3) mlabsize(small) ) 
( rspike lo up xaxis,  lwidth(thin) lcolor(black) horizontal),
legend(off) ytitle(" ") 
yscale(noline) xscale(noline) 
ylabel( 1.8 " "   2 "Voters with Party Differential = 10" 3 "Voters with Party Differential = 0" 3.2 " ", labsize(small) angle(horizontal) noticks nogrid) 
xline(0, lp(shortdash)lwidth(vthin)lcolor(black)  )
xlabel(-.01(.03).3, noticks labsize(vsmall) labcolor(white) )
graphregion(fcolor(white))
saving(balance, replace)
;
#delimit cr


graph export balance.tif, replace
graph export balance.pdf, replace
graph export balance.png, replace
graph export balance.eps, replace preview(on)



/*
capture drop xaxis
gen xaxis = _n in 1
#delimit ;
twoway (scatter xaxis coef,  mcolor(black) mlabel(coef) mlabcolor(black) mlabposition(12) mlabgap(*3) mlabsize(small) ) 
( rspike lo up xaxis,  lwidth(thin) lcolor(black) horizontal),
legend(off) ytitle(" ") 
yscale(noline) xscale(noline) 
ylabel( .8 " " 1 `""Average Effect" "of Absorption""'  1.2 " ", labsize(small) angle(horizontal) noticks nogrid) 
xline(0, lp(shortdash)lwidth(vthin)lcolor(black)  )
xlabel(-.3(.3).3, noticks labsize(vsmall) labcolor(white) )
graphregion(fcolor(white))
saving(ate, replace)
;
#delimit cr
*/



exit



